﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.LIB;
using YArchitech.Revit;
using YJKRevitStairs.StairsEdit.Common;
using YJKRevitStairs.StairsEdit.DataDefine;

namespace YJKRevitStairs.StairsEdit.Forms
{
	public partial class ShowStairsListForm : System.Windows.Forms.Form
	{
		public ShowStairsListForm(ExternalCommandData m_commandData)
		{
			this.InitializeComponent();
			//base.Icon = Resource.YArchitechIcon;
			this.uidoc = m_commandData.Application.ActiveUIDocument;
			this.document = this.uidoc.Document;
			ViewPlan viewPlan = null;
			if ((int)this.uidoc.ActiveView.ViewType == 1 || (int)this.uidoc.ActiveView.ViewType == 2)
			{
				CommonUnit.GetAllSameViewPlanByActiveView(m_commandData, ref this.viewList, ref viewPlan);
			}
		}

		private void InitListViewHeader(int iflg)
		{
			if (iflg == 2)
			{
				this.lstStairs.Columns.Add("楼层名称", 118);
				this.lstStairs.Columns.Add("楼梯名称", 180);
				return;
			}
			this.lstStairs.Columns.Add("楼层名称", 104);
			this.lstStairs.Columns.Add("楼梯名称", 112);
			this.lstStairs.Columns.Add("备注", 92);
		}

		public bool initList(IList<Stairs> rawStairs)
		{
			this.InitListViewHeader(3);
			List<rawStairsInfo> list = new List<rawStairsInfo>();
			list.Clear();
			foreach (Stairs stairs in rawStairs)
			{
				ElementId elementId = stairs.GetParameter(BuiltInParameter.STAIRS_BASE_LEVEL_PARAM).AsElementId();
				string name = (this.document.GetElement(elementId) as Level).Name;
				StairsType stairsType = this.document.GetElement(stairs.GetTypeId()) as StairsType;
				string name2 = stairsType.Name;
				StairsRunType stairsRunType = this.document.GetElement(stairsType.RunType) as StairsRunType;
				double treadThickness = stairsRunType.TreadThickness;
				double riserThickness = stairsRunType.RiserThickness;
				double structuralDepth = stairsRunType.StructuralDepth;
				string errorInfo = "";
				if (treadThickness < CommonUnit._epsDouble || riserThickness < CommonUnit._epsDouble)
				{
					errorInfo = "无贴面层";
				}
				else if (structuralDepth < CommonUnit._epsDouble)
				{
					errorInfo = "无结构层";
				}
				rawStairsInfo NewstairsInfo = new rawStairsInfo(name, name2, errorInfo, stairs.Id.ToString());
				int num = list.FindIndex((rawStairsInfo p) => p.isEqual(NewstairsInfo));
				if (num != -1)
				{
					rawStairsInfo value = list[num];
					value.Add(NewstairsInfo);
					list[num] = value;
				}
				else
				{
					list.Add(NewstairsInfo);
				}
			}
			this.lstStairs.Items.Clear();
			this.lstStairs.BeginUpdate();
			foreach (rawStairsInfo rawStairsInfo in list)
			{
				ListViewItem listViewItem = new ListViewItem(rawStairsInfo.strFloorName);
				listViewItem.SubItems.Add(rawStairsInfo.strStairsType);
				listViewItem.SubItems.Add(rawStairsInfo.strErrorInfo);
				listViewItem.Name = rawStairsInfo.strName;
				this.lstStairs.Items.Add(listViewItem);
			}
			this.lstStairs.EndUpdate();
			return true;
		}

		public bool initList(IList<Stairs> rawStairs, IList<PairsStairs> rawStairsPairs)
		{
			this.InitListViewHeader(2);
			List<rawStairsInfo> list = new List<rawStairsInfo>();
			list.Clear();
			foreach (Stairs stairs in rawStairs)
			{
				ElementId elementId = stairs.GetParameter(BuiltInParameter.STAIRS_BASE_LEVEL_PARAM).AsElementId();
				string name = (this.document.GetElement(elementId) as Level).Name;
				string name2 = (this.document.GetElement(stairs.GetTypeId()) as StairsType).Name;
				string errorInfo = "";
				rawStairsInfo NewstairsInfo = new rawStairsInfo(name, name2, errorInfo, stairs.Id.ToString());
				int num = list.FindIndex((rawStairsInfo p) => p.isEqual(NewstairsInfo));
				if (num != -1)
				{
					rawStairsInfo value = list[num];
					value.Add(NewstairsInfo);
					list[num] = value;
				}
				else
				{
					list.Add(NewstairsInfo);
				}
			}
			foreach (PairsStairs pairsStairs in rawStairsPairs)
			{
				Stairs stairs2 = pairsStairs.stairs1;
				Stairs stairs3 = pairsStairs.stairs2;
				ElementId elementId2 = stairs2.GetParameter(BuiltInParameter.STAIRS_BASE_LEVEL_PARAM).AsElementId();
				string name3 = (this.document.GetElement(elementId2) as Level).Name;
				Element element = this.document.GetElement(stairs2.GetTypeId()) as StairsType;
				StairsType stairsType = this.document.GetElement(stairs3.GetTypeId()) as StairsType;
				string text = element.Name;
				text += ",";
				text += stairsType.Name;
				string errorInfo2 = "";
				string text2 = stairs2.Id.ToString();
				text2 += ",";
				text2 += stairs3.Id.ToString();
				rawStairsInfo NewstairsInfo = new rawStairsInfo(name3, text, errorInfo2, text2);
				int num2 = list.FindIndex((rawStairsInfo p) => p.isEqual(NewstairsInfo));
				if (num2 != -1)
				{
					rawStairsInfo value2 = list[num2];
					value2.Add(NewstairsInfo);
					list[num2] = value2;
				}
				else
				{
					list.Add(NewstairsInfo);
				}
			}
			this.lstStairs.Items.Clear();
			this.lstStairs.BeginUpdate();
			foreach (rawStairsInfo rawStairsInfo in list)
			{
				ListViewItem listViewItem = new ListViewItem(rawStairsInfo.strFloorName);
				listViewItem.SubItems.Add(rawStairsInfo.strStairsType);
				listViewItem.Name = rawStairsInfo.strName;
				this.lstStairs.Items.Add(listViewItem);
			}
			this.lstStairs.EndUpdate();
			return true;
		}

		private void listView1_DoubleClick(object sender, EventArgs e)
		{
			ListViewItem focusedItem = this.lstStairs.FocusedItem;
			string name = focusedItem.Name;
			char[] separator = new char[]
			{
				','
			};
			string[] array = name.Split(separator);
			Selection selection = this.uidoc.Selection;
			RevitVersionFuncs.ClearSelection(selection);
			List<Element> list = new List<Element>();
			ElementSet elementSet = new ElementSet();
			int num = 0;
			while ((long)num < array.LongCount<string>())
			{
				ElementId elementId = new ElementId(int.Parse(array[num]));
				list.Add(this.document.GetElement(elementId));
				elementSet.Insert(this.document.GetElement(elementId));
				num++;
			}
			if ((int)this.uidoc.ActiveView.ViewType == 1 || (int)this.uidoc.ActiveView.ViewType == 2)
			{
				foreach (ViewPlan viewPlan in this.viewList)
				{
					if (viewPlan.GenLevel.Name == focusedItem.SubItems[0].Text)
					{
						this.uidoc.ActiveView = viewPlan;
						break;
					}
				}
			}
			this.uidoc.ShowElements(elementSet);
			RevitVersionFuncs.SetSelection(selection, list);
			this.uidoc.RefreshActiveView();
		}

		private void btn_close_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void ShowStairsListForm_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\u001b')
			{
				base.Close();
			}
		}

		private void lstStairs_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\u001b')
			{
				base.Close();
			}
		}

		private Document document;

		private UIDocument uidoc;

		private IList<ViewPlan> viewList;
	}
}
